<!-- 司机管理 -->
<template>
  <div
    ref="contentData"
    class="dashboard-container  customer-list-box"
  >
    <div class="app-container ">
      <el-card
        class="search-card-box"
        shadow="never"
      >
        <el-form
          ref="driverFormData"
          :model="driverFormData"
          label-width="80px"
        >
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="司机账号:"
                style="margin-bottom: 20px"
                label-width="80px"
                prop="account"
              >
                <el-input v-model="driverFormData.account" placeholder="请输入司机账号" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="司机姓名:"
                style="margin-bottom: 20px"
                label-width="80px"
                prop="name"
              >
                <el-input v-model="driverFormData.name" placeholder="请输入司机姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="司机手机号:"
                style="margin-bottom: 20px"
                label-width="80px"
                prop="phone"
              >
                <el-input v-model="driverFormData.phone" placeholder="请输入司机手机号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="所属机构:"
                style="margin-bottom: 20px"
                label-width="80px"
                prop="agency"
              >
                <el-select
                  v-model="driverFormData.agency"
                  :data="agencyList"
                  style="width: 100%"
                  class="filter-item size"
                  size="small"
                  filterable
                  clearable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in agency"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    class="size"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8" style="padding-left:50px">
              <el-button type="warning" @click="handleFilter('查询')"
              >搜索
              </el-button
              >
              <el-button
                plain
                class="reset-btn"
                @click="resetForm"
              >重置
              </el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <div
        v-loading="listLoading"
        :class="{ 'loading-box': listLoading }"
        element-loading-text="加载中"
        style="margin-top: 20px"
      >
        <el-card shadow="never" class="table-card-box">
          <el-table
            ref="multipleTable"
            :data="dataList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
          >
            <div
              v-show="
                (!dataList || dataList.length <= 0) &&
                  !listLoading &&
                  !searchkey
              "
              slot="empty"
            >
              <img
                src="@/icons/pic-kong@2x.png"
                alt="img"
                style="margin-top: 20px;width: 14%;height: 14%;"
              />
              <p
                style="
          margin-top: -20px;
          padding-bottom: 0px;
          "
              >
                这里空空如也
              </p>
            </div>
            <el-card
              v-show="
                (!dataList || dataList.length <= 0) && !listLoading && searchkey
              "
              slot="empty"
              class="table-empty-box"
              shadow="never"
            >
              <empty></empty>
            </el-card>
            <el-table-column align="left" type="index" label="序号" width="60">
              <template slot-scope="scope">
                <span>{{
                    scope.$index +
                    (driverFormData.page - 1) *
                    driverFormData.pageSize + 1
                  }}</span>
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="司机账号"
              min-width="130"
              prop="account"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="司机姓名"
              min-width="130"
              prop="name"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="司机电话"
              min-width="130"
              prop="mobile"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="关联车辆"
              min-width="130"
              prop="truck.licensePlate"
            >

            </el-table-column>
            <el-table-column
              align="left"
              label="所属机构"
              min-width="130"
              prop="agency.name"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="工作状态"
              min-width="130"
              prop="workStatus"
            >
              <template slot-scope="scope">
                <span v-if="scope.row.workStatus === 0">休息</span>
                <span v-else-if="scope.row.workStatus === 1">上班</span>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              fixed="right"
              label="操作"
              width="190"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="handleDetail(scope.row.userId)"
                >查看
                </el-link>
                <span class="line"></span>
                <span class="line"></span>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="handleConfigDriver(scope.row.driverIds,scope.row.truckId)"
                >配置车辆
                </el-link>
              </template>
            </el-table-column>
          </el-table>
          <!-- end -->

          <!-- 分页 -->
          <div v-show="dataList && dataList.length > 0" class="pagination">
            <div class="pages">
              <el-pagination
                :current-page="Number(driverFormData.page)"
                :total="Number(total)"
                :page-size="Number(driverFormData.pageSize)"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </div>
        </el-card>
      </div>
      <!--    配置司机-->
      <el-dialog
        title="配置司机"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <el-card v-if="dialogVisible">
          <el-form ref="vehicleDetailList" :model="basicData" label-width="100px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="司机编号">
                  <label>{{ basicData.number }}</label>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="司机名称">
                  <label>{{ basicData.name }}</label>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="司机电话">
                  <label>{{ basicData.mobile }}</label>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属机构">
                  <label>{{ basicData.agency.name }}</label>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <el-dialog
          title="配置司机"
          :visible.sync="dialogVisible"
          width="50%"
          :before-close="handleClose"
        >
          <el-card>
            <el-form ref="vehicleDetailList" :model="vehicleDetailList" label-width="100px">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="司机编号">
                    <label>{{ }}</label>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="司机名称">
                    <label>{{ }}</label>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="司机电话">
                    <label></label>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="所属机构">
                    <label></label>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-card>
          <el-form>
            <el-form-item label="配置司机" prop="id">
              <el-select placeholder="请选择" v-model="vehicleDetailList" @selection-change="handleSelectionChange">
                <el-option />

                <!--                v-for="item in vehicleDetailList"-->
                <!--                :key="item.id"-->
                <!--                :label="item.name"-->
                <!--                :value="item.id"-->
              </el-select>
            </el-form-item>
          </el-form>
          <el-table
            ref="multipleTable"
            :data="vehicleList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
          >
            <div
              v-show="
                (!vehicleList || vehicleList.length <= 0) &&
                  !listLoading &&
                  !searchkey
              "
              slot="empty"
            >
              <img
                src="@/icons/pic-kong@2x.png"
                alt="img"
                style="margin-top: 20px;width: 14%;height: 14%;"
              />
              <p
                style="
              margin-top: -20px;
              padding-bottom: 0px;
              "
              >
                这里空空如也
              </p>
            </div>
            <el-card
              v-show="
                (!vehicleList || vehicleList.length <= 0) && !listLoading && searchkey
              "
              slot="empty"
              class="table-empty-box"
              shadow="never"
            >
              <empty></empty>
            </el-card>
            <el-table-column align="left" label="车牌号" min-width="80"></el-table-column>
            <el-table-column align="left" label="车辆状态" min-width="80"></el-table-column>
            <el-table-column
              align="center"
              fixed="right"
              label="操作"
              width="120"
              class-name="small-padding fixed-width"
            >
              <el-col width="100%" slot-scope="scope">
                <!-- 调用的编辑方法 -->
                <el-button
                  @click="handleEditDriver(scope.row)"
                  class="customer-add-btn"
                  style="color:#419EFF;border: none;padding: 0; margin: 0;background-color:transparent;"
                >编辑
                </el-button>
                <span class="line"></span>
                <el-button
                  @click="handleDelDriver(scope.row.id)"
                  class="customer-add-btn"
                  style="border: none;padding: 0; margin: 0;background-color:transparent;color:#F56C6C;"
                >删除
                </el-button>
              </el-col>
            </el-table-column>
          </el-table>
          <span slot="footer" class="dialog-footer" style="text-align: center;">
            <el-button type="danger" @click="submitDriverConfig">确 定</el-button>
            <el-button @click="dialogVisible = false">取 消</el-button>
          </span>
        </el-dialog>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addVehicleBinding, getBindingDrivers, getDriver, configureVehicle } from '@/api/vehicle'
import { getDriverList } from '@/api/driver'

export default {
  name: 'Courses',
  data() {
    return {
      driverFormData: {
        page: 1,
        pageSize: 10,
        name: '',
        userId: ''
      },
      agencyList: [],
      driverChooseList: [],
      driverChooseListChoice: [],
      agency: [],
      vehicleList: [],
      basicData: {},
      dataList: [],
      listLoading: false,
      searchkey: '',
      dialogVisible: false,
      total: null,
      vehicleDetailList: {}
    }
  },
  mounted() {
    this.getDriverChooseList()
    this.getAgencyList()
  },
  methods: {
    submitDriverConfig(id) {
      addVehicleBinding({ 'truckId': this.selectTrunkId, 'driverIds': [this.driverList.id] })
        .then(res => {
          console.log('保存成功', res)
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '保存成功'
            })
            this.getVehicleDetailList(this.selectTrunkId)
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            })
          }
        })
        .catch(err => {
          this.$message({
            type: 'error',
            message: err.msg
          })
        })
      // this.dialogVisible = false
    },
    // 打开弹窗
    handleDispatch() {
      this.dialogVisible = true
    },
    // 关闭弹窗
    handleClose() {
      this.dialogVisible = false
    },
    handleSelectionChange(selection) {
      getBindingDrivers(selection)
        .then(res => {
          this.driverChooseListChoice = res.data
        })
      // this.driverChooseListChoice = selection
    },
    async getList(id) {
      const res = await getDriver(id)
      this.basicData = res.data
    },
    handleConfigDriver(driverIds, truckId) {
      const h = this.$createElement
      this.$msgbox({
        title: '配置车辆',
        message: h('p', null, [
          h('p', null, '配置车辆需满足以下条件： '),
          h('i', { style: 'color: #333' }, '1 司机信息已完善'),
          h('i', { style: 'color: #333', paddingLeft: '40px' }, '2 司机已设置排班'),
          h('p', { style: 'color: #333' }, '3 司机绑定车辆未在启用状态')
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            this.dialogVisible = true
            const data = {
              driverIds: driverIds,
              truckId: truckId
            }
            const res = configureVehicle(data)
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '配置成功'
              })
              this.getVehicleDetailList(truckId)
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
            done()
          } else {
            done()
          }
        }
      })
    },
    // 配置车辆

    // 车型详情
    handleDetail(id) {
      this.$router.push({
        path: '/transit/driver-detail',
        query: { id: id }
      })
    },
    async getDriverChooseList() {
      this.listLoading = true
      const params = { ...this.driverFormData }
      const { data: res } = await getDriverList(params)
      this.listLoading = false
      // 获取数据给dataList
      this.dataList = res.items
      console.log(this.dataList)
      // 获取总条数
      this.total = res.counts
    },
    getAgencyList() {
      this.agencyList = this.dataList.map(item => item.agency)
    },
    // 搜索信息
    handleFilter() {
      this.driverFormData.page = 1
      this.getDriverChooseList(this.driverFormData)
      this.searchkey = true
    },
    // 每页显示信息条数
    handleSizeChange(val) {
      this.driverFormData.pageSize = val
      if (this.driverFormData.page === 1) {
        this.getDriverChooseList(this.driverFormData)
      }
    },
    // 进入某一页
    handleCurrentChange(val) {
      this.driverFormData.page = val
      this.getDriverChooseList()
    },
    // 重置
    resetForm(driverSearchFormData) {
      // 之所以注释掉是因为这里必须配合prop对表单进行赋值，但后端有的字段不支持空串
      // this.$refs[driverSearchFormData].resetFields()
      this.driverFormData = {
        page: 1,
        pageSize: 10,
        status: this.driverFormData.status
      }
      this.searchkey = false
      this.getDriverChooseList()
    }

  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
label {
  color: #818693;
}

.vehicle-detail {
  ::v-deep .el-container {
    flex-direction: column;
    margin-left: 0px;

  }

  .in {
    height: calc(100vh - 205px);
    // height: 100%;
  }

  .aside-box {
    background: #ffffff;
    border-radius: 4px;
    // width: 150px;
    padding: 24px 37px 24px 37px;
    box-sizing: border-box;

    // min-height: calc(100vh - 50px);
    ::v-deep .el-aside {
      padding-bottom: 14px;
      width: 100% !important;
      display: flex;
      background-color: #ffffff;
      border-bottom: 1px solid #E5E7EC;
      text-align: left;
      font-size: 14px;

      .aside-item {
        // margin-top: 38px;
        cursor: pointer;

        &:first-child {
          margin-right: 53px;
          padding-left: 25px;

        }
      }

      .aside-item.active {
        font-size: 16px;
        font-weight: bold;
        color: #20232A;

        &:after {
          content: '';
          display: block;
          background-color: #E15536;
          height: 3px;
          width: 49px;
          margin: 0 auto;
          position: relative;
          top: 14px;
        }
      }
    }
  }
}

.card-box {
  width: 1190px;
  height: 800px !important;
  margin: 0 auto;
}

.tab {
  margin-left: 20px !important;
}

::v-deep .el-tabs__nav {
  .is-active {
    color: #333 !important;
    font-weight: 700 !important;
  }

  .el-tabs__active-bar {
    background-color: #E15536;
  }
}

.fist-box {
  position: relative;
  height: 650px;
  width: 100%;
}

.el-footer {
  position: absolute;
  padding: 20px;
  width: 100%;
  left: 0px;
  bottom: 0px;
  text-align: center;
  border-top: 1px solid #E5E7EC;
}

.img-box {
  width: 100px;
  height: 100px;
  border: 1px solid #D8DDE3;
  border-radius: 4px;
  text-align: center;
  line-height: 100px;
}

::v-deep .el-form-item__label {
  text-align: left;
}

.line {
  display: inline-block;
  width: 1px;
  height: 12px;
  background: #E4E7ED;
  margin: 0 4px;
  align-items: baseline;
  margin-top: 8px;
}
</style>

